var requestService = require('../../utils/requestService.js');
var app=getApp();
Page({
    data: {
        currentPayMethod:'wallet',
        totalAmount:'0.00',
        orderNo:'',
        paymentMethods: [/*{
            method:'wechat',
            icon: '../images/ico_wechat.png',
            text: '微信支付',
            checked:true
        }, */{
            method:'wallet',
            icon: '../images/ico_wallet.png',
            text: '我的钱包',
            checked:true
        }],
        paymentImmediateBt:{
            disabled: false,
            loading: false
        }
    },
    // 页面加载
    onLoad: function (options) {
        // 设置本页导航标题
        wx.setNavigationBarTitle({
            title: '确认支付'
        })
        const totalAmount=options.totalAmount;
        const orderNo=options.orderNo;
        this.setData({
            totalAmount:totalAmount,
            orderNo:orderNo
        })
    },
    /**选择支付方式*/
    checkPayMethod:function(event) {
        const method=event.currentTarget.id;
        const paymentMethods=this.data.paymentMethods;
        paymentMethods.forEach(function (item) {
            if(item.method==method){
                item.checked=true
            }else{
                item.checked=false
            }
        });
        this.setData({
            currentPayMethod:method,
            paymentMethods:paymentMethods
        })
    },
    /**立即支付*/
    paymentImmediate:function () {
        let that=this;
        that.paymentImmediateBtLoading();
        const currentPayMethod=that.data.currentPayMethod;
        const orderNo=that.data.orderNo;//订单编号
        if(currentPayMethod=="wechat"){
            that.paymentImmediateBtUnLoading();
            wx.navigateTo({
                url: "../msg/msg_success?msg=微信支付成功&redirectUrl=../payment/payment_detail&btText=订单详情&orderNo="+orderNo
            })
        }else{
            requestService.sendPostRequest("/payment/ewallet/topay/"+orderNo, null, function (result) {
                const resultData = result.data;
                that.paymentImmediateBtUnLoading();
                if( resultData.resCode == 0 && resultData.resData == true){
                    wx.redirectTo({
                        url: "../msg/msg_success?msg=订单支付成功&redirectUrl=../payment/payment_detail&btText=订单详情&param1="+orderNo
                    })
                }else{
                    wx.navigateTo({
                        url: '../msg/msg_fail?msg='+resultData.message
                    })
                }
            },function () {
                that.paymentImmediateBtUnLoading();

            });

        }
    },
    /**按钮设置为加载、不可点击*/
    paymentImmediateBtLoading:function () {
        let that=this;
        that.setData({
            "paymentImmediateBt.disabled": true,
            "paymentImmediateBt.loading": true
        });
    },
    /**按钮取消加载、不可点击*/
    paymentImmediateBtUnLoading:function () {
        let that=this;
        that.setData({
            "paymentImmediateBt.disabled": false,
            "paymentImmediateBt.loading": false
        });
    }


})